<template>
    <div>
        <a-modal
            :visible="visible"
            @ok="handleOk"
            okText="提交"
            @cancel="visible = false"
            width="100%" 
            :maskClosable="false"
            class="supplementary-rectification-model"
        >
            <template slot="title">
                <div style="width: 100%;display: flex;text-align: center;justify-content: center;">
                    <span style="font-size: 24px;font-weight: 600;text-align: center;">{{ formState.modalTitle }}</span>
                </div>
            </template>
            <a-spin class="supplementary-spin" tip="正在提交中..." :spinning="spinning">
            </a-spin>
            <div style="display: flex;" v-if="!spinning">
                <a-card size="small" :title="formState.opinionName?formState.opinionName:(formState.opinionText?formState.opinionText:formState.modalTitle+'意见')" style="width: 40%">
                    <div :key="opinionKey">
                        <div>
                            <a-button style="margin-right: 15px;margin-bottom: 10px;" class="blue-btn" type="primary" @click="synchronousClick()">同步到文档</a-button>
                        </div>
                        <a-textarea 
                            v-model:value="formState[typeData.opinion]"
                            placeholder="请输入意见"
                            :autosize="{ minRows: 8, maxRows: 10000 }"
                        ></a-textarea>
                        <div style="display: flex;justify-content: space-between;margin-top: 20px;" v-if="formState.isTime">
                            <div class="time-limit-warning-v">
                                <span>
                                    <span>{{ formState.modalTitle }}</span>
                                    <span>时限（工作日）：</span>
                                </span>
                                <a-input-number id="inputNumber1" v-model="formState[typeData.timeLimit]" @change="formState.fyjsx = formState[typeData.timeLimit]-3" :min="0" :max="30" placeholder="最多30日"/>
                            </div>
                            <div class="time-limit-warning-v">
                                <span>
                                    <span>{{ formState.modalTitle }}</span>
                                    <span>预警（工作日）：</span>
                                </span>
                                <a-input-number id="inputNumber2" v-model="formState.fyjsx" :min="0" :max="30" placeholder="最多30日"/>
                            </div>
                        </div>
                        <div style="margin-bottom: 10px;margin-top: 10px;display: flex;align-items: center;" v-if="formState.isJssc">
                            <span>技术审查：</span>
                            <a-radio-group defaultValue="不符合" :options="[{label: '不符合', value: '不符合'}]"/>
                        </div>
                        <div v-if="formState.isSPCYTextMessage">
                            <div class="time-limit-warning-v" style="margin-bottom: 10px;margin-top: 20px;display: flex;align-items: center;">
                                <span>{{formState.SPCYmessageTitle?formState.SPCYmessageTitle:'发送短信提醒'}}：</span>
                                <a-radio-group v-model="formState[typeData.SPCYsmgflag?typeData.SPCYsmgflag:'smgflag']" :options="[{label: '是', value: '是'},{label: '否', value: '否'}]"/>
                            </div>
                        </div>
                        <div v-if="formState.isTextMessage">
                            <div class="time-limit-warning-v" style="margin-bottom: 10px;margin-top: 20px;display: flex;align-items: center;">
                                <span>{{formState.messageTitle?formState.messageTitle:'发送短信提醒企业'}}：</span>
                                <a-radio-group v-model="formState[typeData.smgflag?typeData.smgflag:'fsffsdx']" :options="[{label: '是', value: '是'},{label: '否', value: '否'}]"/>
                            </div>
                            <a-textarea 
                                v-if="!formState.isFdxnr && formState[typeData.smgflag?typeData.smgflag:'fsffsdx'] == '是'"
                                v-model:value="formState[typeData.dxnrKey?typeData.dxnrKey:'fdxnr']"
                                placeholder="请输入短信内容"
                                :autosize="{ minRows: 3, maxRows: 10000 }"
                            ></a-textarea>
                        </div>
                    </div>
                </a-card>
                <a-card size="small" :title="formState.filterTitle" style="flex: 1;">
                    <div style="margin-bottom: 10px;">
                        <!-- <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="refreshFile()">刷新</a-button>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="downloadFiles(iframeData)">下载</a-button>
                        <a-button v-if="iframeData.fwjlx == '.pdf'" style="margin-right: 15px;" class="blue-btn" type="primary" @click="reEdit(iframeData)">重新编辑</a-button>
                        <a-button v-if="iframeData.fwjlx !== '.pdf'" style="margin-right: 15px;" class="blue-btn" type="primary" @click="saveFiles(iframeData)">保存</a-button>
                        <a-button v-if="iframeData.fwjlx !== '.pdf'" style="margin-right: 15px;" class="blue-btn" type="primary" @click="generatePDFClick(iframeData)">生成PDF</a-button> -->
                        <a-button v-if="iframeData.id"  style="margin-right: 15px;"  class="red-btn" type="primary"  @click="producerDelete(iframeData)" icon="delete">删除</a-button>
                        <a-upload v-else style="display: inline-block;" :beforeUpload="$event=>{return false}" :showUploadList="false" action="" @change="singleUploadCahnge($event,'补充资料通知函')">
                            <a-button
                            class="blue-btn" 
                            style="width: 124px;"
                            type="primary" icon="upload">上传</a-button>
                        </a-upload>
                    </div>
                    <div>
                        <a-spin tip="正在加载中..." :spinning="iframeSpinning">
                            <iframe :id="frameId" :src="iframeSrc" width="100%" :height="windowInnerHeight+'px'"  v-if="iframeSrc" :title="formState.filterTitle" :name="formState.filterTitle"></iframe>
                            <div v-else style="display: flex;justify-content: center;margin: 20px 0;">
                                <a-empty description="暂无文件或文件发生错误！"/>
                            </div>
                        </a-spin>
                    </div>
                </a-card>
            </div>
            <!-- </a-spin> -->
        </a-modal>
    </div>
</template>

<script>
import blobType from '@/common/blobType'
import {
    pushQyBcZgNotice,
    pushQyBqbzNotice,
    returnZwfwxt,
    qycjsqlcsubmitFlow,
    getLastNodeBlyj,
    spcygetSummaryOpinionInScyj,
    initWjgetZzzBqbzFile,
    getWebOfficeDownloadFile,
    getDocxToPdf,
    againEditPdfOnlineFile,
    webOfficeApiopenFile,
    initWjinitZzzBqbzFile,
    spcysubmitFlowByZwfw,
    initWjinitQycjFile,
    initWjgetZzzQycjFile,
    spcySqsxWjjlgetLocalFileById,
    spcySqsxWjjldeleteFileById,
    initWjuploadQxtztzs,
} from './service/index'
import YchConstant from '@/api/YchConstant'
export default {
    components:{

    },
    props:{

    },
    data(){
        return{
            formState:{},
            typeData:{},
            visible:false,
            interfaceData:{
                pushQyBcZgNotice:pushQyBcZgNotice,
                pushQyBqbzNotice:pushQyBqbzNotice,
                returnZwfwxt:returnZwfwxt,
                qycjsqlcsubmitFlow:qycjsqlcsubmitFlow,
                spcysubmitFlowByZwfw:spcysubmitFlowByZwfw,
            },
            
            spinning:false,
            opinionKey:null,
            frameId:'frameId'+new Date().getTime(),
            iframeSrc:'',
            iframeData:{},
            windowInnerHeight:window.innerHeight - 253,
            fileInterFacesData:{
                initWjinitZzzBqbzFile:initWjinitZzzBqbzFile,//补齐补正
                initWjinitQycjFile:initWjinitQycjFile,//退回政务
                
                initWjgetZzzBqbzFile:initWjgetZzzBqbzFile,//补齐补正
                initWjgetZzzQycjFile:initWjgetZzzQycjFile,//退回政务
            },
            iframeSpinning:false,
        }
    },
    watch:{

    },
    mounted(){
        
    },
    methods:{
        async getData(val,typeData){
            this.iframeSrc = ''
            this.spinning = false
            let data = {//用来写初始化数据
                modalTitle:'补齐补正',
                isTextMessage:true,
                fsffsdx:'是',
            }
            this.typeData = typeData
            data[typeData.smgflag?typeData.smgflag:'fsffsdx'] = '是'
            data[typeData.SPCYsmgflag?typeData.SPCYsmgflag:'smgflag'] = '是'
            // this.fileInterFacesData = {
            //     initWjinitZzzBqbzFile:initWjinitZzzBqbzFile,//补齐补正
            //     initWjinitQycjFile:initWjinitQycjFile,//退回政务
                
            //     initWjgetZzzBqbzFile:initWjgetZzzBqbzFile,//补齐补正
            //     initWjgetZzzQycjFile:initWjgetZzzQycjFile,//退回政务
            // }
            this.formState = {...data,...val}
            if(this.formState.modalTitle == '补齐补正'){
                await spcygetSummaryOpinionInScyj({sqsxid:val.fsqsxid}).then(res=>{
                    if(res.code == 200){
                        
                        this.formState[typeData.opinion] = res.result
                        //console.log(this.formState,res)
                        this.opinionKey = Date.now()
                    }else{
                        this.formState[typeData.opinion] = ''
                    }
                })
            }
            if(val.department == '化妆品'){
                await getLastNodeBlyj({sqsxid:val.fsqsxid}).then(res=>{
                    if(res.code == 200){
                        this.formState[typeData.opinion] = res.result.blyj
                    }
                })
            }
            this.$nextTick(()=>{
                this.synchronousClick(1)
            })
            
        },
        synchronousClick(val){
            //console.log('没数据吗？',this.formState,this.typeData.opinion)
            //console.log(this.fileInterFacesData)
            if(!this.formState[this.typeData.opinion] && !val){
                this.$message.warning('请输入意见')
                return
            }
            if(this.formState.isGetFilter){
                this.fileInterFacesData[this.formState.initJk]({opinion:this.formState[this.typeData.opinion],sqsxid:this.formState.fsqsxid}).then(res=>{
                    if(res.code == 200){
                        this.getNoticeFliter()
                        if(!val){
                            this.$message.success(res.message)
                        }
                    }else{
                        if(!val){
                            this.$message.warning(res.message)
                        }
                        
                    }
                })
            }
            
        },
        getNoticeFliter(){
            
            if(this.formState.isGetFilter){
                //补齐补正通知函
                this.iframeSpinning = true
                this.iframeSrc = ''
                this.fileInterFacesData[this.formState.fileJk]({sqsxid:this.formState.fsqsxid}).then(res=>{
                    if(res.code == 200){
                        //console.log('补齐补正通知函',res.result)
                        
                        if(res.result.length){
                            this.iframeData = res.result[0]
                            if(this.iframeData.fwjlx == '.doc' || this.iframeData.fwjlx == '.docx'){
                                this.openFileClick(this.iframeData)
                            }else{
                                this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.iframeData.id
                                this.iframeSpinning = false
                            }
                        }else{
                            this.iframeData = {}
                            this.iframeSrc = ''
                            this.iframeSpinning = false
                        }
                    }else{
                        this.iframeSrc = ''
                        this.iframeSpinning = false
                    }
                    
                })
            }
        },
        //删除
        producerDelete(val,type){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    spcySqsxWjjldeleteFileById({id:val.id}).then(res=>{
                        if(res.code == 200){
                            that.getNoticeFliter()
                        }else{
                            that.$message.warning(res.message)
                        }
                    })
                },
                
            });
            
        },
        //上传
        singleUploadCahnge({file},val){
            var that = this
            if(file.name.indexOf('.pdf') !== -1 ){
                const fileData = new FormData();
                fileData.append("file",file);
                fileData.append("sqsxid",this.formState.fsqsxid);
                fileData.append("type",val);
                initWjuploadQxtztzs(fileData).then(res => {
                    if(res.code !== 200){
                        that.$message.warning('请上传pdf文件或者联系管理员!')
                    }else{
                        that.getNoticeFliter()
                    }
                    
                })
            }else{
                this.$message.warning('请上传pdf文件')
            }
            
        },
        refreshFile(){//刷新
            this.getNoticeFliter()
        },
        openFileClick(val){//打开文件
            webOfficeApiopenFile({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.iframeSrc = res.result.wordUrl
                    this.iframeKey = Date.now()
                }else{
                    this.$message.warning(res.message)
                }
                this.iframeSpinning = false
            })
        },
        downloadFiles(val){//下载
            var that = this
            var fwjlx = val.fwjlx
            let param = { fywflcode:'001002',id:val.id,sqsxid:this.formState.fsqsxid}
            spcySqsxWjjlgetLocalFileById(param).then((data) => {
                if (!data) {
                    that.$message.warning('文件下载失败')
                return
                }
                    let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf(fwjlx)] }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', val.fwjccmc )
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
            })
        },
        reEdit(val){//重新编辑
            this.iframeSpinning = true
            this.iframeSrc = ''
            againEditPdfOnlineFile({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
        saveFiles(val){//保存
            this.iframeSpinning = true
            this.iframeSrc = ''
            getWebOfficeDownloadFile({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
        generatePDFClick(val){//生成PDF
            this.iframeSpinning = true
            this.iframeSrc = ''
            getDocxToPdf({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
        handleOk(){
            // this.$store.dispatch('UpdateGlobalLoading', {tipName:'正在提交中...',spinning:true})
            this.spinning = true
            this.interfaceData[this.formState.interfaceValue](this.formState).then(res=>{
                if(res.code == 200){
                    this.$emit('rectificationSuccess',this.formState)
                    this.visible = false
                    this.$message.success(res.message)
                    
                }else{
                    this.$message.warning(res.message)
                }
                this.spinning = false
                // this.$store.dispatch('UpdateGlobalLoading', {spinning:false})
            })
            
        },
    }
}
</script>
<style lang="less">
.supplementary-rectification-model{
    .supplementary-spin{
        position: absolute;
        width: 100%;
        z-index: 99;
        height: 100%;
        top: 0;
        left: 0;
        padding-top: 102px;
        background: rgba(0,0,0,0.3);
        opacity: 1;
        
        .ant-spin-dot i{
            background: #5e7aef;
        }
        .ant-spin-text{
            color: #5e7aef;
        }
    }
    .ant-modal {
        max-width: 100%;
        top: 0;
        padding-bottom: 0;
        margin: 0;
    }
    .ant-modal-content {
        display: flex;
        flex-direction: column;
        height: calc(100vh);
    }
    .ant-modal-body {
        flex: 1;
        padding: 8px;
    }
    .ant-card-head{
        background-image: linear-gradient(to bottom, #e7f3ff, #fff, #e7f3ff, #e7f3ff, #e7f3ff, #e7f3ff, #e7f3ff, #e7f3ff, #e7f3ff);
        background-size: 100% 100% !important;
    }
    .ant-card-head-title{
        color: #38f;
        font-weight: 600;
        font-size: 18px;
    }
    .ant-card-body{
        padding: 8px;
    }
        
}
</style>